using System.Collections.Generic;
using LitJson2;
using Net.Common;
using UnityEngine;

/****************************************************
// 功能：
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/26 11:24:18
*****************************************************/

public class HeroController : BaseController<HeroController>
{
    public LoadHeroWindow loadHeroWindow;
    private int lastPercent = 0;

    public override void InitController()
    {
        base.InitController();
        //Debug.Log("HeroController Init");
    }

    /// <summary>
    /// 打开选英雄界面
    /// </summary>
    public void OnHeroSelectWindow()
    {
        ui.Open(WindowName.HeroSelect);
        ui.DestoryWindow(WindowName.Room);
    }

    /// <summary>
    /// 加载战斗英雄界面、地图场景
    /// </summary>
    /// <param name="msgInfo"></param>
    public void OnLoadBattleHero(MsgInfo msgInfo)
    {
        var hero = JsonUtility.FromJson<HeroResponse>(msgInfo.data);
        model.mapId = hero.mapId;
        model.battleHero = hero.battleHero;

        for (var i = 0; i < hero.battleHero.Count; i++)
        {
            if (!hero.battleHero[i].channelId.Equals(model.userInfo.channelId)) { continue; }
            model.currentIndex = i;
        }
        
        //model.currentIndex = 0;

        audioManager.StopBgMusic();
        ui.Open(WindowName.LoadHero);
        ui.DestoryWindow(WindowName.HeroSelect);

        resource.AsyncScene($"map_{hero.mapId}", SceneLoadProgress, BattleController.Instance.InitBattleUI);
    }

    /// <summary>
    /// 显示地图场景加载进度
    /// </summary>
    /// <param name="value"></param>
    private void SceneLoadProgress(float value)
    {
        var percent = (int) (value * 100);
        if (lastPercent == percent) return;
        SendByTcp(RequestType.LOAD_HERO_PROGRESS, new HeroRequest()
        {
            roomId = model.roomId,
            percent = percent,
            type = (int) RequestType.LOAD_HERO_PROGRESS
        });
        lastPercent = percent;
    }

    /// <summary>
    /// 刷新战斗地图加载进度
    /// </summary>
    /// <param name="msgInfo"></param>
    public void OnLoadHeroProgress(MsgInfo msgInfo)
    {
        var percentList = JsonMapper.ToObject<List<int>>(msgInfo.data);
        loadHeroWindow.RefreshProgressData(percentList);
    }
}
